﻿using API.Order;
using API.Product;
using API.Store;
using API.Sys;
using Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text;

namespace API
{
    public class ServiceProxy
    {
        private static CookieContainer cokieContainer = new CookieContainer();
        private static ApiSystem mApiSystem = null;
        private static ApiProduct mApiProduct = null;
        private static ApiStore mApiStore = null;
        private static ApiOrder mApiOrder = null;
        static string serverUrl = ConfigurationManager.AppSettings["ServerUrl"];
        public static ApiSystem ApiSystemProxy()
        {
            try
            {
                if (mApiSystem == null)
                {
                    mApiSystem = new ApiSystem();
                    mApiSystem.CookieContainer = cokieContainer;
                    mApiSystem.EnableDecompression = true;
                }
                UriBuilder ub = new UriBuilder(serverUrl+"/WebAPI/WebService/ApiSystem.asmx");
                mApiProduct.Url = ub.Uri.ToString();
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("ApiSystemProxy", ex);
            }
            return mApiSystem;
        }
        public static ApiProduct ApiProductProxy()
        {
            try
            {
                if (mApiProduct == null)
                {
                    mApiProduct = new ApiProduct();
                    mApiProduct.CookieContainer = cokieContainer;
                    mApiProduct.EnableDecompression = true;
                }
                UriBuilder ub = new UriBuilder(serverUrl + "/WebAPI/WebService/ApiProduct.asmx");
                mApiProduct.Url = ub.Uri.ToString();
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("ApiProductProxy", ex);
            }
            return mApiProduct;
        }
        public static ApiStore ApiStoreProxy()
        {
            try
            {
                if (mApiStore == null)
                {
                    mApiStore = new ApiStore();
                    mApiStore.CookieContainer = cokieContainer;
                    mApiStore.EnableDecompression = true;
                }
                UriBuilder ub = new UriBuilder(serverUrl + "/WebAPI/WebService/ApiStore.asmx");
                mApiProduct.Url = ub.Uri.ToString();
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("ApiStoreProxy", ex);
            }
            return mApiStore;
        }
        public static ApiOrder ApiOrderProxy()
        {
            try
            {
                if (mApiOrder == null)
                {
                    mApiOrder = new ApiOrder();
                    mApiOrder.CookieContainer = cokieContainer;
                    mApiOrder.EnableDecompression = true;
                }
                UriBuilder ub = new UriBuilder(serverUrl + "/WebAPI/WebService/ApiOrder.asmx");
                mApiProduct.Url = ub.Uri.ToString();
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("ApiOrderProxy", ex);
            }
            return mApiOrder;
        }
    }
}
